package org.example.coursemangementserver.server.service.impl.admin;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.coursemangementserver.common.constant.JwtClaimsConstant;
import org.example.coursemangementserver.common.context.BaseContext;
import org.example.coursemangementserver.common.exception.common.CommonException;
import org.example.coursemangementserver.common.properties.JwtProperties;
import org.example.coursemangementserver.common.utils.Aes;
import org.example.coursemangementserver.common.utils.JwtUtil;
import org.example.coursemangementserver.pojo.dto.AdminDeleteStudentSelectInfoDTO;
import org.example.coursemangementserver.pojo.dto.IDListDTO;
import org.example.coursemangementserver.pojo.dto.Pagination;
import org.example.coursemangementserver.pojo.dto.UserLoginDTO;
import org.example.coursemangementserver.pojo.dto.course.*;
import org.example.coursemangementserver.pojo.dto.course.CourseListQuery.GetCourseListDTO;
import org.example.coursemangementserver.pojo.dto.course.CourseListQuery.QueryParams;
import org.example.coursemangementserver.pojo.dto.course.EnrollmentListQuery.GetEnrollmentListDTO;
import org.example.coursemangementserver.pojo.dto.course.StudentSelectRecordListQuery.GetStudentSelectRecordListDTO;
import org.example.coursemangementserver.pojo.dto.student.AddStudentInfoDTO;
import org.example.coursemangementserver.pojo.dto.student.EditStudentInfoDTO;
import org.example.coursemangementserver.pojo.dto.student.StudentListQuery.AdminDeleteStudentDTO;
import org.example.coursemangementserver.pojo.dto.student.StudentListQuery.AdminPassStudentDTO;
import org.example.coursemangementserver.pojo.dto.teacher.AddTeacherInfoDTO;
import org.example.coursemangementserver.pojo.dto.teacher.AdminDeleteTeacherDTO;
import org.example.coursemangementserver.pojo.dto.teacher.AdminPassTeacherDTO;
import org.example.coursemangementserver.pojo.dto.teacher.EditTeacherInfoDTO;
import org.example.coursemangementserver.pojo.entity.*;
import org.example.coursemangementserver.pojo.vo.UserLoginVO;
import org.example.coursemangementserver.pojo.vo.course.CommentCourseStatistics.CommentCourseStatisticVO;
import org.example.coursemangementserver.pojo.vo.course.CommentCourseStatistics.CommentCourseStatisticsVO;
import org.example.coursemangementserver.pojo.vo.course.CourseIdWithName.CourseIdWithNameListVO;
import org.example.coursemangementserver.pojo.vo.course.CourseIdWithName.CourseIdWithNameVO;
import org.example.coursemangementserver.pojo.vo.course.CourseList.CourseListVO;
import org.example.coursemangementserver.pojo.vo.course.CourseList.CourseVO;
import org.example.coursemangementserver.pojo.vo.course.EnrollmentList.EnrollmentListVO;
import org.example.coursemangementserver.pojo.vo.course.EnrollmentList.EnrollmentVO;
import org.example.coursemangementserver.pojo.vo.course.SelectCourseStatistics.SelectCourseStatisticVO;
import org.example.coursemangementserver.pojo.vo.course.SelectCourseStatistics.SelectCourseStatisticsVO;
import org.example.coursemangementserver.pojo.vo.course.StudentCourseList.StudentCourseVO;
import org.example.coursemangementserver.pojo.vo.course.StudentSelectRecordList.StudentSelectRecordListVO;
import org.example.coursemangementserver.pojo.vo.course.StudentSelectRecordList.StudentSelectRecordVO;
import org.example.coursemangementserver.pojo.vo.course.TeacherCourseRatingStatistics.TeacherCourseRatingStatisticVO;
import org.example.coursemangementserver.pojo.vo.course.TeacherCourseRatingStatistics.TeacherCourseRatingStatisticsVO;
import org.example.coursemangementserver.pojo.vo.teacher.TeacherIdWithName.TeacherIdWithNameListVO;
import org.example.coursemangementserver.pojo.vo.teacher.TeacherIdWithName.TeacherIdWithNameVO;
import org.example.coursemangementserver.server.mapper.*;
import org.example.coursemangementserver.server.service.interfac.admin.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 12438
 * 教务人员业务逻辑处理类
 */
@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private EnrollmentMapper enrollmentMapper;
    @Autowired
    private SelectionInformationMapper selectionInformationMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseReviewMapper courseReviewMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 注入JWT令牌相关属性
     */
    @Autowired
    private JwtProperties jwtProperties;


    /**
     * 教务人员登录
     * @param userLoginDTO
     * @return
     */
    @Override
    public UserLoginVO login(UserLoginDTO userLoginDTO) {
        Admin admin = adminMapper.selectByUsername(userLoginDTO.getUsername());
        String password = Aes.decrypt(userLoginDTO.getPassword());
        if (admin !=null && password.equals(admin.getPassword())){
            // 登录成功，生成JWT令牌
            Map<String,Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.USER_ID,admin.getAdminId());
            claims.put(JwtClaimsConstant.USERNAME,admin.getCount());
            String jwtToken = JwtUtil.createJwt(claims,jwtProperties.getMangerSecretKey(), jwtProperties.getExpireTime());
            return new UserLoginVO(admin.getAdminId(),admin.getCount(), jwtToken,2,"教务人员","https://qiniu.suqi.store/images/15665270-2549-4c3c-9dcb-f2072ed3676d.jpg");
        } else {
            // 登录失败，抛出异常
            throw new CommonException("密码或者用户名错误,教务人员登录失败");
        }
    }

    /**
     * 根据条件查询获取所有课程列表
     */
    @Override
    public CourseListVO getCourseList(GetCourseListDTO getCourseListDTO) {
        Pagination pagination = getCourseListDTO.getPagination();
        Integer start = (pagination.getCurrentPage()-1)* pagination.getPageSize();
        QueryParams queryParams = getCourseListDTO.getQueryParams();
        if (queryParams != null){
            // 查询参数不为空，则进行带有查询参数的分页查询
            // 得到表中的课程总数
            Integer total = courseMapper.getCourseTotal(queryParams.getName());
            List<CourseVO> courseList = courseMapper.getCourseList(start,pagination.getPageSize(),queryParams.getName());
            return new CourseListVO(total,courseList);
        }
        Integer total = courseMapper.getCourseTotal(null);
        List<CourseVO>  courseList = courseMapper.getCourseList(start,pagination.getPageSize(),null);
        return new CourseListVO(total,courseList);
    }

    /**
     * 更新课程信息
     * @param updateCourseDTO
     */
    @Override
    public void updateCourseByCourseId(UpdateCourseDTO updateCourseDTO) {
        Course course = courseMapper.getCourseById(updateCourseDTO.getCourseId());
        course.setName(updateCourseDTO.getName());
        course.setCredits(updateCourseDTO.getCredits());
        course.setDescription(updateCourseDTO.getDescription());
        course.setCourseImage(updateCourseDTO.getCourseImage());
        courseMapper.updateById(course);
    }

    /**
     * 获取所有待选课程信息
     * @param getEnrollmentListDTO
     * @return
     */
    @Override
    public EnrollmentListVO getEnrollmentList(GetEnrollmentListDTO getEnrollmentListDTO) {
        Pagination pagination = getEnrollmentListDTO.getPagination();
        Integer start = (pagination.getCurrentPage()-1)* pagination.getPageSize();
        org.example.coursemangementserver.pojo.dto.course.EnrollmentListQuery.QueryParams queryParams = getEnrollmentListDTO.getQueryParams();
        if (queryParams != null){
            // 查询参数不为空，则进行带有查询参数的分页查询
            // 得到表中的课程总数
            Integer total = enrollmentMapper.getEnrollmentTotal(queryParams.getName(), queryParams.getTeacherName());
            List<EnrollmentVO>  courseList = enrollmentMapper.getEnrollmentList(start,pagination.getPageSize(),queryParams.getName(), queryParams.getTeacherName());
            return new EnrollmentListVO(total,courseList);
        }
        Integer total = enrollmentMapper.getEnrollmentTotal(null,null);
        List<EnrollmentVO>  courseList = enrollmentMapper.getEnrollmentList(start,pagination.getPageSize(),null,null);
        return new EnrollmentListVO(total,courseList);

    }

    /**
     * 教务人员进行结课操作
     * @param endCourseDTO
     */
    @Override
    public void adminEndCourse(EndCourseDTO endCourseDTO) {
        // 修改待选课程记录表
        Enrollment enrollment = enrollmentMapper.getEnrollmentById(endCourseDTO.getEnrollmentId());
        enrollment.setStatus(-1);
        enrollmentMapper.updateById(enrollment);
        // 修改学生选课记录表,将selectionInformation表中所有这个EnrollmentId为adminEndCourseDTO.getEnrollmentId()的数据的status改为2
        // 创建条件构造器，指定需要修改的记录的条件
        UpdateWrapper<SelectionInformation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("enrollment_id", endCourseDTO.getEnrollmentId())
                .set("status", 2);
        // 直接在条件构造器中设置要修改的字段和新值

        // 使用条件构造器执行更新操作
        selectionInformationMapper.update(null, updateWrapper);

        Course course = courseMapper.getCourseById(enrollment.getCourseId());
        Integer credit = course.getCredits();
        // 根据学生选课记录表中已修改的数据的student_id字段，增加students表中对应学生的学分

        // 通过该从待选课程中选了课的学生的ID列表
        List<Long> studentIds = selectionInformationMapper.getStudentsByEnrollmentId(endCourseDTO.getEnrollmentId());
        for (Long studentId : studentIds) {
            // 增加学生的学分
            Student student = studentMapper.selectById(studentId);
            if (student != null) {
                student.setLearnCredits(student.getLearnCredits() + credit);
                studentMapper.updateById(student);
            }
        }
    }

    /**
     * 教务人员查询所有学生的所有选课记录
     * @param getStudentSelectRecordListDTO
     * @return
     */
    @Override
    public StudentSelectRecordListVO getStudentSelectRecordList(GetStudentSelectRecordListDTO getStudentSelectRecordListDTO) {
        Pagination pagination = getStudentSelectRecordListDTO.getPagination();
        Integer start = (pagination.getCurrentPage()-1)* pagination.getPageSize();
        org.example.coursemangementserver.pojo.dto.course.StudentSelectRecordListQuery.QueryParams queryParams = getStudentSelectRecordListDTO.getQueryParams();
        if (queryParams != null){
            // 查询参数不为空，则进行带有查询参数的分页查询
            // 得到表中的课程总数
            Integer total = selectionInformationMapper.getStudentSelectRecordTotal(queryParams.getStudentName(),queryParams.getName());
            List<StudentSelectRecordVO>  studentSelectRecordList = selectionInformationMapper.getStudentSelectRecordList(start,pagination.getPageSize(),queryParams.getStudentName(),queryParams.getName());
            return new StudentSelectRecordListVO(total,studentSelectRecordList);
        }
        Integer total = selectionInformationMapper.getStudentSelectRecordTotal(null,null);
        List<StudentSelectRecordVO>  studentSelectRecordList = selectionInformationMapper.getStudentSelectRecordList(start,pagination.getPageSize(),null,null);
        return new StudentSelectRecordListVO(total,studentSelectRecordList);
    }

    /**
     * 获取课程评价的统计数据
     * @return
     */
    @Override
    public CommentCourseStatisticsVO getCourseCommentAverageRatings() {
        List<CommentCourseStatisticVO> courseCommentAverageRatings = courseReviewMapper.getCommentCourseStatistics();
        return new CommentCourseStatisticsVO(courseCommentAverageRatings);
    }

    /**
     * 获取选课统计
     * @return
     */
    @Override
    public SelectCourseStatisticsVO getSelectCourseStatisticsVO() {
        List<SelectCourseStatisticVO>  selectCourseStatistics = enrollmentMapper.getSelectCourseStatistics();
        return new SelectCourseStatisticsVO(selectCourseStatistics);
    }

    /**
     * 教务人员获取教师课程评价统计数据
     * @return
     */
    @Override
    public TeacherCourseRatingStatisticsVO getTeacherCourseAverageRatings() {
        List<TeacherCourseRatingStatisticVO> teacherCourseAverageRatings = courseReviewMapper.getTeacherCourseStatistics();
        return new TeacherCourseRatingStatisticsVO(teacherCourseAverageRatings);
    }

    /**
     * 删除课程信息
     * @param deleteCourseDTO
     */
    @Override
    public void deleteCourseByCourseId(DeleteCourseDTO deleteCourseDTO) {
        // 进行一些条件判断
        Course course = courseMapper.getCourseById(deleteCourseDTO.getCourseId());
        course.setStatus(-1);
        courseMapper.updateById(course);
    }

    @Override
    public void passTeacherInfo(AdminPassTeacherDTO adminPassTeacherDTO) {
        Teacher teacher = teacherMapper.getTeacherById(adminPassTeacherDTO.getTeacherId());
        teacher.setStatus(1);
        teacherMapper.updateById(teacher);
    }

    /**
     * 教务人员审核拒绝通过教师信息
     * @param adminDeleteTeacherDTO
     */
    @Override
    public void deleteTeacherInfo(AdminDeleteTeacherDTO adminDeleteTeacherDTO) {
        Teacher teacher = teacherMapper.getTeacherById(adminDeleteTeacherDTO.getTeacherId());
        teacher.setStatus(-1);
        teacherMapper.updateById(teacher);
    }

    @Override
    public void passStudentInfo(AdminPassStudentDTO adminPassTeacherDTO) {
        Student student = studentMapper.getStudentById(adminPassTeacherDTO.getStudentId());
        student.setStatus(1);
        studentMapper.updateById(student);
    }

    @Override
    public void deleteStudentInfo(AdminDeleteStudentDTO adminDeleteTeacherDTO) {
        Student student = studentMapper.getStudentById(adminDeleteTeacherDTO.getStudentId());
        student.setStatus(-1);
        studentMapper.updateById(student);
    }

    @Override
    public void deleteStudentOnList(IDListDTO idListDTO) {
        for (Long id: idListDTO.getIds()){
            Student student = studentMapper.getStudentById(id);
            student.setStatus(-1);
            studentMapper.updateById(student);
        }
    }

    @Override
    public void deleteTeacherOnList(IDListDTO idListDTO) {
        for(Long id: idListDTO.getIds()){
            Teacher teacher = teacherMapper.getTeacherById(id);
            teacher.setStatus(-1);
            teacherMapper.updateById(teacher);
        }
    }

    /**
     * 教务人员删除某个学生的选课信息
     * @param adminDeleteStudentSelectInfoDTO
     */
    @Override
    public void deleteStudentSelectInfo(AdminDeleteStudentSelectInfoDTO adminDeleteStudentSelectInfoDTO) {
        SelectionInformation selectionInformation = selectionInformationMapper.getSelectionInformationById(adminDeleteStudentSelectInfoDTO.getSelectInfoId());
        selectionInformation.setStatus(-1);
        selectionInformationMapper.updateById(selectionInformation);
        // TODO 删除和结课不同，不需要增加学生的学分，只需恢复选课表中的容量
        Enrollment enrollment = enrollmentMapper.getEnrollmentById(selectionInformation.getEnrollmentId());
        enrollment.setPresentNum(enrollment.getPresentNum()-1);
    }

    /**
     * 教务人员新增学生信息
     * @param addStudentInfoDTO
     */
    @Override
    public void addStudentInfo(AddStudentInfoDTO addStudentInfoDTO) {
        Student student = new Student();
        BeanUtils.copyProperties(addStudentInfoDTO,student);
        student.setStatus(1);
        student.setPassword(Aes.decrypt(addStudentInfoDTO.getPassword()));
        studentMapper.insert(student);
    }

    /**
     * 教务人员新增老师信息
     * @param addTeacherInfoDTO
     */
    @Override
    public void addTeacherInfo(AddTeacherInfoDTO addTeacherInfoDTO) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(addTeacherInfoDTO,teacher);
        teacher.setStatus(1);
        teacher.setPassword(Aes.decrypt(addTeacherInfoDTO.getPassword()));
        teacherMapper.insert(teacher);
    }

    @Override
    public void editStudentInfo(EditStudentInfoDTO editStudentInfoDTO) {
        Student student = studentMapper.getStudentById(editStudentInfoDTO.getStudentId());
        student.setName(editStudentInfoDTO.getName());
        student.setStudentNum(editStudentInfoDTO.getStudentNum());
        student.setAvatarUrl(editStudentInfoDTO.getAvatarUrl());
        student.setGrade(editStudentInfoDTO.getGrade());
        if (editStudentInfoDTO.getPassword() != null){
            student.setPassword(Aes.decrypt(editStudentInfoDTO.getPassword()));
        }
        studentMapper.updateById(student);
    }

    /**
     * 教务人员编辑老师信息
     * @param editTeacherInfoDTO
     */
    @Override
    public void editTeacherInfo(EditTeacherInfoDTO editTeacherInfoDTO) {
        Teacher teacher = teacherMapper.getTeacherById(editTeacherInfoDTO.getTeacherId());
        teacher.setName(editTeacherInfoDTO.getName());
        teacher.setTeacherNum(editTeacherInfoDTO.getTeacherNum());
        teacher.setAvatarUrl(editTeacherInfoDTO.getAvatarUrl());
        if (editTeacherInfoDTO.getPassword() != null){
            teacher.setPassword(Aes.decrypt(editTeacherInfoDTO.getPassword()));
        }
        teacherMapper.updateById(teacher);
    }

    @Override
    public void addCourse(AddCourseDTO addCourseDTO) {
        Course course = new Course();
        BeanUtils.copyProperties(addCourseDTO,course);
        if (!Objects.equals(addCourseDTO.getCourseImage(), "")){
            course.setCourseImage(addCourseDTO.getCourseImage());
        }  else {
            course.setCourseImage(null);
        }
        courseMapper.insert(course);
    }

    @Override
    public void deleteCurseOnList(IDListDTO idListDTO) {
        for (Long id: idListDTO.getIds()){
            // 进行一些条件判断
            Course course = courseMapper.getCourseById(id);
            course.setStatus(-1);
            courseMapper.updateById(course);
        }
    }

    @Override
    public void deleteStudentSelectInfoOnList(IDListDTO idListDTO) {
        for(Long id: idListDTO.getIds()){
            SelectionInformation selectionInformation = selectionInformationMapper.getSelectionInformationById(id);
            selectionInformation.setStatus(-1);
            selectionInformationMapper.updateById(selectionInformation);
            // 删除和结课不同，不需要增加学生的学分，只需恢复选课表中的容量
            Enrollment enrollment = enrollmentMapper.getEnrollmentById(selectionInformation.getEnrollmentId());
            enrollment.setPresentNum(enrollment.getPresentNum()-1);
        }
    }

    /**
     * 前台获取所有课程的ID和对应的名字信息
     * @return
     */
    @Override
    public CourseIdWithNameListVO getCourseIdWithName() {
        List<CourseIdWithNameVO>  courseIdWithNameList = courseMapper.getCourseIdWithName();
        return new CourseIdWithNameListVO(courseIdWithNameList);
    }

    /**
     * 前台获取所有老师的ID和对应的名字信息
     * @return
     */
    @Override
    public TeacherIdWithNameListVO getTeacherIdWithName() {
        List<TeacherIdWithNameVO>  teacherIdWithNameList = teacherMapper.getTeacherIdWithName();
        return new TeacherIdWithNameListVO(teacherIdWithNameList);
    }

    @Override
    public void addSelectCourse(AddSelectCourseDTO addSelectCourseDTO) {
        Long teacherId = addSelectCourseDTO.getTeacherId();
        Integer onClass = addSelectCourseDTO.getOnClass();
        Integer week = addSelectCourseDTO.getWeek();
        Enrollment enrollment = enrollmentMapper.getEnrollmentByCondition(teacherId,onClass,week);
        if (enrollment != null){
            throw new CommonException("老师时间冲突，课堂添加失败");
        }
        Enrollment e = new Enrollment();
        BeanUtils.copyProperties(addSelectCourseDTO,e);
        enrollmentMapper.insert(e);
        if (addSelectCourseDTO.getIsAutoAdd() == 1){
            // 自动为所有符合条件的学生添加该课程
            // 找到该年级中的所有正常的学生的ID列表
            List<Long> ids = studentMapper.findStudentIdsByGrade(addSelectCourseDTO.getGrade());
            for (Long id: ids){
                // 获取进行选课的学生的信息
                Student student = studentMapper.getStudentById(id);
                // 获取待选课程信息
                Enrollment  enrollment4 = enrollmentMapper.getEnrollmentById(e.getEnrollmentId());
                // 进行验证，看是否容量超过了，以及学生年级是否符合要求
                if (enrollment4.getMaxNum() <= enrollment4.getPresentNum()){
                    // 容量超过了,抛出异常
                    continue;
                }
                if (!Objects.equals(student.getGrade(), enrollment4.getGrade())){
                    // 年级不符合要求，抛出异常
                    continue;
                }
                if (!(enrollment4.getMinCredits()<=student.getLearnCredits() && student.getLearnCredits()<=enrollment4.getMaxCredits())){
                    // 学分不符合要求，抛出异常
                    continue;
                }
                // 查看它所选的已有课程是否会与该课程产生冲突
                Integer total = selectionInformationMapper.getStudentCourseTotal(BaseContext.getCurrentUser().getUserId(),null,null);
                List<StudentCourseVO> studentCourseList = selectionInformationMapper.getStudentCourseList(0,total,BaseContext.getCurrentUser().getUserId(),null,null);
                Integer weekP = enrollment4.getWeek();
                Integer onClassk = enrollment4.getOnClass();
                boolean flag = false;
                for (StudentCourseVO studentCourseVO : studentCourseList) {
                    if (Objects.equals(studentCourseVO.getWeek(), weekP) && Objects.equals(studentCourseVO.getOnClass(), onClassk)){
                        // 冲突，抛出异常   TODO
                        flag = true;
                    }
                }
                if (flag) {
                    continue;
                }
                // 进行选课
                enrollment4.setPresentNum(enrollment4.getPresentNum()+1);
                enrollmentMapper.updateById(enrollment4);
                // 查找是否原先有选课记录
                SelectionInformation selectionInformation = selectionInformationMapper.getSelectionInformationByEnrollmentIdAndStudentId(student.getStudentId(),enrollment4.getEnrollmentId());
                if (selectionInformation != null){
                    // 有选课记录，修改选课记录的状态为正常即可，不用新增
                    selectionInformation.setStatus(1);
                    selectionInformationMapper.updateById(selectionInformation);
                    continue;
                }
                // 生成选课记录
                selectionInformation = new SelectionInformation();
                selectionInformation.setCourseId(enrollment4.getCourseId());
                selectionInformation.setStudentId(student.getStudentId());
                selectionInformation.setEnrollmentId(enrollment4.getEnrollmentId());
                selectionInformationMapper.insert(selectionInformation);
            }
        }
    }

    @Override
    public void updateSelectCourse(UpdateSelectCourseDTO addSelectCourseDTO) {
        Enrollment e = enrollmentMapper.getEnrollmentById(addSelectCourseDTO.getEnrollmentId());
        BeanUtils.copyProperties(addSelectCourseDTO,e);
        enrollmentMapper.updateById(e);
    }

    @Override
    public void adminStopSelectCourse(AdminStopSelectCourseDTO adminStopSelectCourseDTO) {
        Enrollment enrollment = enrollmentMapper.getEnrollmentById(adminStopSelectCourseDTO.getEnrollmentId());
        enrollment.setSelectStatus(-1);
        enrollmentMapper.updateById(enrollment);
    }
    @Override
    public void endEnrollmentOnList(IDListDTO idListDTO){
        for(Long id:idListDTO.getIds()){
            // 修改待选课程记录表
            Enrollment enrollment = enrollmentMapper.getEnrollmentById(id);
            enrollment.setStatus(-1);
            enrollmentMapper.updateById(enrollment);
            // 修改学生选课记录表,将selectionInformation表中所有这个EnrollmentId为adminEndCourseDTO.getEnrollmentId()的数据的status改为2
            // 创建条件构造器，指定需要修改的记录的条件
            UpdateWrapper<SelectionInformation> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("enrollment_id", id)
                    .set("status", 2);
            // 直接在条件构造器中设置要修改的字段和新值

            // 使用条件构造器执行更新操作
            selectionInformationMapper.update(null, updateWrapper);

            Course course = courseMapper.getCourseById(enrollment.getCourseId());
            Integer credit = course.getCredits();
            // 根据学生选课记录表中已修改的数据的student_id字段，增加students表中对应学生的学分

            // 通过该从待选课程中选了课的学生的ID列表
            List<Long> studentIds = selectionInformationMapper.getStudentsByEnrollmentId(id);
            for (Long studentId : studentIds) {
                // 增加学生的学分
                Student student = studentMapper.selectById(studentId);
                if (student != null) {
                    student.setLearnCredits(student.getLearnCredits() + credit);
                    studentMapper.updateById(student);
                }
            }
        }
    }
}
